const BaseRest = require('../rest.js');
module.exports = class extends BaseRest
{
    constructor(ctx)
    {
        super(ctx);
        this.stepsModel = this.model('apiv1/competitionSteps');
    }


    async stepWorksCountAndApplicationsCountAction()
    {
        let workCount = await this.model('apiv1/competitionsteptoworks').getWorksCountByWorkStepsGuid(this._guid);
        let applicationCount = await this.model('apiv1/application').getApplicationsCountByStepGuid(this._guid);
        let data = {
            worksCount: workCount,
            applicationCount: applicationCount
        };
        return this.responseQuery(data);
    }

    async stepPauseAction()
    {
        let res = await this.stepsModel.where({PK_CompetitionStepsGuid: this._guid}).update({Status: '已暂停'});
        return this.resopnseUpdate(res);
    }


    async stContinueAction()
    {
        let res = await this.stepsModel.where({PK_CompetitionStepsGuid: this._guid}).update({Status: '进行中'});
        return this.resopnseUpdate(res);
    }

    async stEndAction()
    {
        let res = await this.stepsModel.where({PK_CompetitionStepsGuid: this._guid}).update({Status: '已结束'});
        return this.resopnseUpdate(res);
    }

    async cpdetailfromstAction()
    {
        return this.responseQuery(
            (await  this.stepsModel.setRelation('competition').find(this._guid)).competition
        );
    }

    async stdetailAction()
    {
        return this.responseQuery(await this.stepsModel.setRelation(['stepsaward', 'competitionstepstojudger']).find(this._guid));
    }

    async delstepAction()
    {
        return this.responseDelete(await  this.stepsModel.delete(this._guid))
    }


    async updatecpstepsAction()
    {
        //数据验证
        let postData = think.stringToJson(this.post('data'));
        if (postData === false)
        {
            return this.responseInfo(52004, '提交的表单信息错误,该数据必须为json格式,请检查数据格式,上传数据格式为{\'data\',yourJsonData},如 : $.post("http://127.0.0.1:8091/apiv1/competition",{\'data\':yourJsonData}),yourJsonData为你要上传的数据包');
        }
        let baseRules = {
            cpguid: {type: 'string'},
            steps: {type: 'array'}
        };
        let bbase = think.validate(postData, baseRules);
        if (bbase !== true) return this.error(52040, bbase[0].message);
        //todo 未完成
        //postData为验证成功后的数据

        //获取比赛的guid
        let cpguid = postData.cpguid;


        let steps = this.parseSteps(postData.steps);

        // return this.resopnseUpdate(await this.cpModel.where({PK_CompetitionGuid:cpguid}).update(postData));
    }

    parseSteps(steps)
    {

    }
};